﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Interfaces;
using Map;

namespace Porter
{
    public static class Global
    {
        public static string ApplicationPath = System.IO.Path.GetDirectoryName(new Uri(
                                                System.Reflection.Assembly.GetExecutingAssembly().GetName().
                                                   CodeBase).LocalPath);

        public static MapControl map;
        public static MiddlewareServices m_middleware;
        public static Service m_service;
        public static Settings Settings = new Settings(ApplicationPath + "\\settings.xml");
        public static FavoriteStore Favorites;

        public static Dictionary<VehicleType, int> VehicleMarkerCache = new Dictionary<VehicleType, int>();
        public static long UserPositionMarkerId;

        public static List<long> CurrentMarkerIds = new List<long>();

        public static void Init()
        {
            string ApplicationDataPath = "\\Application Data\\pozr\\";
            if (FlashCardUtils.FlashCardExists())
            {
                ApplicationDataPath = String.Format("\\{0}\\Application Data\\pozr\\", FlashCardUtils.GetFirstFlashCardPath());
            }
            Settings.Load();
            #region Initialize the default parameters
            Settings.InitDefault("DefaultFilter", "User", TypeCode.String, Settings.MetaType.Text,
                                 new List<object>() { "bus" }, new string[] { "Default filter for results" });
            Settings.InitDefault("MaxResults", "User", TypeCode.Int32, Settings.MetaType.Numeric,
                                 new List<object>() { "99" }, new string[] { "Maximum number of displayed results", "", "" });
            Settings.InitDefault("DefaultDistance", "Location", TypeCode.Int32, Settings.MetaType.Numeric,
                                 new List<object>() { 3 }, new string[] { "Default search radius", "in hundreds of meters", "Specify radius x100 m" });
            Settings.InitDefault("ApplicationDataPath", "System", TypeCode.String, Settings.MetaType.Text,
                                 new List<object>() { ApplicationDataPath }, new string[] {"Teszt"});
            Settings.InitDefault("ServerURL", "System", TypeCode.String, Settings.MetaType.Text,
                                 new List<object>() { "http://lbss.tmit.bme.hu/transportr/interface_v6/index.php" }, new string[] { "Server URL" });
            #endregion

            m_middleware = new MiddlewareServices();
            m_service = new Service(new Uri(Global.Settings.Get("ServerURL") as string));
            map = new MapControl(String.Format("{0}MapCache", Settings.Get("ApplicationDataPath" as string)));
            Favorites = new FavoriteStore();

            #region Initialize vehicle markers
            VehicleMarkerCache[VehicleType.LocalBus] = map.AddPrototype(new MarkerDescriptor() { Offset = Porter.Graphics.Vehicle.Marker.Offset, BgImageStream = Porter.Graphics.Vehicle.Marker.GetStream(VehicleType.LocalBus) });
            VehicleMarkerCache[VehicleType.Metro] = map.AddPrototype(new MarkerDescriptor() { Offset = Porter.Graphics.Vehicle.Marker.Offset, BgImageStream = Porter.Graphics.Vehicle.Marker.GetStream(VehicleType.Metro) });
            VehicleMarkerCache[VehicleType.Suburban] = map.AddPrototype(new MarkerDescriptor() { Offset = Porter.Graphics.Vehicle.Marker.Offset, BgImageStream = Porter.Graphics.Vehicle.Marker.GetStream(VehicleType.Suburban) });
            VehicleMarkerCache[VehicleType.Train] = map.AddPrototype(new MarkerDescriptor() { Offset = Porter.Graphics.Vehicle.Marker.Offset, BgImageStream = Porter.Graphics.Vehicle.Marker.GetStream(VehicleType.Train) });
            VehicleMarkerCache[VehicleType.Tram] = map.AddPrototype(new MarkerDescriptor() { Offset = Porter.Graphics.Vehicle.Marker.Offset, BgImageStream = Porter.Graphics.Vehicle.Marker.GetStream(VehicleType.Tram) });
            VehicleMarkerCache[VehicleType.Unknown] = map.AddPrototype(new MarkerDescriptor() { Offset = Porter.Graphics.Vehicle.Marker.Offset, BgImageStream = Porter.Graphics.Vehicle.Marker.GetStream(VehicleType.Unknown) });
            #endregion

            /* center the map to our position if available */
            if (m_middleware.LastFixPosition.Longitude != Double.NaN && m_middleware.LastFixPosition.Latitude != Double.NaN)
            {
                UserPositionMarkerId = map.AddMarker(m_middleware.LastFixPosition.Latitude, m_middleware.LastFixPosition.Longitude,
                                                     new MarkerDescriptor()
                                                     {
                                                         BgImageStream = Graphics.Marker.GetStream(MarkerType.Red),
                                                         Offset = Graphics.Marker.GetOffset(MarkerType.Red)
                                                     });
                map.Fit(map.MaxZoom, new GeoPoint(m_middleware.LastFixPosition.Latitude, m_middleware.LastFixPosition.Longitude));
            }
        }

        public static void Dispose()
        {
            map.Dispose();
        }
    }
}
